<template>
  <div class="app-container">
    <el-form :model="formData" label-width="120px" label-position="left">
      <el-form-item label="测试题目">
        <el-input v-model="formData.title"></el-input>
      </el-form-item>
      <el-form-item label="排序">
        <el-input v-model="formData.sort"></el-input>
      </el-form-item>

      <div v-for="(item,index) in formData.answerList" :key="'answer'+index">
        <el-form-item :label="'答案' + (index+1)">
          <el-input v-model="item.content"></el-input>
        </el-form-item>
        <el-form-item :label="'答案' + (index+1) + '标签'">
          <el-select v-model="selectLabel[index]" multiple collapse-tags>
            <el-option v-for="item in lableList" :key="item.labelId" :label="item.labelName" :value="item.labelId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <el-button type="danger" @click.native.prevent="removeAnswer(index)">删除答案</el-button>
        </el-form-item>
      </div>

    </el-form>
    <div class="submit-box">
      <el-button type="primary" :loading="loading" style="width: 200px;" @click.native.prevent="doSubmit">提交</el-button>
      <el-button @click.native.prevent="addAnswer">新增答案</el-button>
    </div>
  </div>
</template>

<script>
  import { create } from '@/api/intention_test_question'
  import { getAll } from '@/api/label'
  import { errorMsg, successMsg } from '@/utils/my'

  export default {
    data() {
      return {
        loading: false,
        formData: {
          title: '',
          sort: '',
          answerList: []
        },
        selectLabel: [],
        lableList: []
      }
    },

    created() {
      this.fetchLabelList()
    },

    methods: {

      fetchLabelList() {
        const that = this
        getAll().then(res => {
          if (res.code != 20000) {
            return errorMsg(res.message)
          }
          that.lableList = res.data
        })
      },

      doSubmit() {
        const that = this
        let d = that.formData

        if (d.title.length <= 0) {
          return errorMsg('请输入测试题目')
        }
        if (d.answerList.length <= 0) {
          return errorMsg('请至少新增一个答案')
        }

        for (let i = 0; i < d.answerList.length; i++) {
          d.answerList[i].labelIds = that.selectLabel[i].join(',')
        }

        that.loading = true

        create(d).then(res => {
          that.loading = false
          if (res.code != 20000) {
            return errorMsg(res.message)
          }
          successMsg(res.message)
          setTimeout(function() {
            that.$router.push({path: '/admin/intention-test-question'})
          }, 1500)
        })
      },

      addAnswer() {
        this.selectLabel[this.formData.answerList.length] = []
        this.formData.answerList.push({
          content: '',
          labelIds: ''
        })
      },

      removeAnswer(index) {
        this.selectLabel.splice(index, 1)
        this.formData.answerList.splice(index, 1)
      }

    }
  }
</script>

<style>
  .submit-box {
    height: 50px;
    line-height: 50px;
    text-align: center;
    padding-right: 20px;
  }
</style>
